<?php


namespace App\Http\Controllers\Wechat;


use App\Models\AppletsIndexGoodsList;
use App\Models\AppletsIndexListCategory;
use App\Models\Collection;
use App\Models\ActivityGoods;
use App\Services\CommonService;
use App\Services\ActivityService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class MyCentreController extends BaseController
{
    /**
     * 个人中心推荐分类商品列表
     * @param Request $request
     * @return false|string
     */
    public function indexList(Request $request){
        $user = $this->member;
        $page = $request->input('page','1');
        $pageSize = $request->input('pageSize',AppletsIndexGoodsList::CACHE_LIMIT_NUM);
        $category = Redis::get(AppletsIndexListCategory::REDIS_FIRST_CATEGORY_KEY);
        $category = json_decode($category,true);
        if(empty($category)){
            $category = AppletsIndexListCategory::getFirstList();
        }
        $cateId = 0;
        if($category){
            $cateId = $category['id'];
        }
        //缓存
        $goods = AppletsIndexGoodsList::getList($cateId,$page,$pageSize);

        if($goods['data']){
            // var_dump($goods['data']);
//            foreach ($goods['data'] as $k=>$v){
//                $activity_goods = ActivityGoods::getGoodsActivity($v['goods_id']);
//
//                if(!empty($activity_goods)){
//                    foreach($activity_goods as $key=>$value){
//                        $uid = explode(',',$value['uid']);
//                        if(in_array($user['id'],$uid)){
//                            $activity_goods[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_YES;
//                        }else{
//                            $activity_goods[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_NO;
//                        }
//
//                        $min_price = ceil($goods['data'][$k]['min_price'] * 30);
//
//                        if($value['is_code'] == ActivityService::ACTIVITY_CODE_ONE){
//                            //满减券
//                            if($min_price>=$value['full']){
//
//                                $firstRent_new = $min_price-$value['money'];
//
//                            }else{
//                                $firstRent_new = $min_price;
//
//                            }
//                        }else if($value['is_code'] == ActivityService::ACTIVITY_CODE_TWO){
//                            //折扣券
//                            $money = $value['money']/10;
//
//                            $firstRent_new = $min_price * $money;
//
//
//                        }else if($value['is_code'] == ActivityService::ACTIVITY_CODE_THREE){
//                            //固定金额券
//                            $firstRent_new = $value['money'];
//
//                        }
//                        $goods['data'][$k]['activity_title'] = $value['title'];
//                    }
//                    $goods['data'][$k]['dayRent_new'] = sprintf("%.2f",$firstRent_new/30);
//                }
//
//
//            }
            Redis::hset(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page,json_encode($goods));
        }
         //缓存
        $goods = Redis::hget(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page);
        if($goods){
            $goods = json_decode($goods,true);
        }else{

            $goods = AppletsIndexGoodsList::getList($cateId,$page,$pageSize);

            if($goods['data']){
                // var_dump($goods['data']);
//                foreach ($goods['data'] as $k=>$v){
//                    $activity_goods = ActivityGoods::getGoodsActivity($v['goods_id']);
//
//                    if(!empty($activity_goods)){
//                        foreach($activity_goods as $key=>$value){
//                            $uid = explode(',',$value['uid']);
//                            if(in_array($user['id'],$uid)){
//                                $activity_goods[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_YES;
//                            }else{
//                                $activity_goods[$key]['is_have'] = ActivityService::ACTIVITY_MEMBER_NO;
//                            }
//
//                            $min_price = ceil($goods['data'][$k]['min_price'] * 30);
//
//                            if($value['is_code'] == ActivityService::ACTIVITY_CODE_ONE){
//                                //满减券
//                                if($min_price>=$value['full']){
//
//                                    $firstRent_new = $min_price-$value['money'];
//
//                                }else{
//                                    $firstRent_new = $min_price;
//
//                                }
//                            }else if($value['is_code'] == ActivityService::ACTIVITY_CODE_TWO){
//                                //折扣券
//                                $money = $value['money']/10;
//
//                                $firstRent_new = $min_price * $money;
//
//
//                            }else if($value['is_code'] == ActivityService::ACTIVITY_CODE_THREE){
//                                //固定金额券
//                                $firstRent_new = $value['money'];
//
//                            }
//                            $goods['data'][$k]['activity_title'] = $value['title'];
//                        }
//                        $goods['data'][$k]['dayRent_new'] = sprintf("%.2f",$firstRent_new/30);
//                    }
//
//
//                }
                Redis::hset(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page,json_encode($goods));
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
    }




}
